/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aburakc.network;

import aburakc.message.GeneralMessage;
import aburakc.message.SMSTextMessage;
import java.util.Date;

/**
 *
 * @author burak.costu
 */
public class MessageProcessor {

    public static String toDeliverMessage(SMSTextMessage message) {
        StringBuffer sb = new StringBuffer();
        sb.append(message.getAddress());
        sb.append("||");
        sb.append(message.getMessage());
        sb.append("||");
        sb.append(message.getDate().getTime());
        sb.append("|&&|");
        return sb.toString();
    }

    public static String toSmsSendMessage(SMSTextMessage message) {
        StringBuffer sb = new StringBuffer();
        sb.append(message.getAddress());
        sb.append("||");
        sb.append(message.getMessage());
        sb.append("||");
        sb.append(message.getDate().getTime());
        sb.append("|&&|");
        return sb.toString();
    }

    public static void main(String[] args) {
        toSmsTextMessage("");
    }

    public static SMSTextMessage toSmsTextMessage(String message) {
        //message = "2||1225621||123456789||12365478||mesaj|&&|";

        int typeIndex = message.indexOf(GeneralMessage.SEPERATOR);
        String strType = message.substring(0, typeIndex);
        int idIndex = message.indexOf(GeneralMessage.SEPERATOR, typeIndex + 2);
        String strId = message.substring(typeIndex + 2, idIndex);
        int type = Integer.parseInt(strType);

        int addressIndex = message.indexOf(GeneralMessage.SEPERATOR, idIndex + 2);
        String address = message.substring(idIndex + 2, addressIndex);
        int dateIndex = message.indexOf(GeneralMessage.SEPERATOR, addressIndex + 2);
        String strDate = message.substring(addressIndex + 2, dateIndex);
        int messageIndex = message.indexOf(GeneralMessage.SEPERATOR, addressIndex + 2);
        int endIndex = message.indexOf(GeneralMessage.FINAL_SEPERATOR);
        String strMessage = message.substring(messageIndex + 2, endIndex);
        
        SMSTextMessage textMessage = new SMSTextMessage();
        textMessage.setMessageId(Long.parseLong(strId));
        textMessage.setAddress(address);
        textMessage.setDate(new Date(Long.parseLong(strDate)));
        textMessage.setMessage(strMessage);
        
        return textMessage;

    }
}
